package com.wazzapps.notifications;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.SystemClock;
import android.util.Base64;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.facebook.internal.logging.monitor.MonitorLogServerProtocol;
import com.unity3d.player.R;
import com.unity3d.player.UnityPlayer;

public class UnityNotificationManager extends BroadcastReceiver {
    private static final String TAG = "UnityNotification";
    private static final String unityClassDefault = "com.unity3d.player.UnityPlayerActivity";

    public static void SendNotification(int i, long j, String str, String str2, int i2, String str3, String str4, boolean z, boolean z2, String str5, String str6) {
        Activity activity = UnityPlayer.currentActivity;
        Intent intent = new Intent(activity, UnityNotificationManager.class);
        intent.putExtra("ticker", str4);
        intent.putExtra("title", str);
        intent.putExtra("message", str2);
        intent.putExtra("smallBitmap", i2);
        intent.putExtra("bigBitmap", str3);
        intent.putExtra("id", i);
        intent.putExtra("sound", z);
        intent.putExtra("vibrate", z2);
        intent.putExtra("activity", str6);
        intent.putExtra("key", str5);
        ((AlarmManager) activity.getSystemService(NotificationCompat.CATEGORY_ALARM)).set(2, SystemClock.elapsedRealtime() + j, PendingIntent.getBroadcast(activity, i, intent, 134217728));
        Log.d(TAG, "Scheduled notification " + i + " after " + j + "ms");
    }

    @Override // android.content.BroadcastReceiver
    public void onReceive(Context context, Intent intent) {
        Class<?> cls;
        NotificationManager notificationManager;
        Notification notification;
        Log.d(TAG, "Received notification");
        NotificationManager notificationManager2 = (NotificationManager) context.getSystemService("notification");
        String stringExtra = intent.getStringExtra("ticker");
        String stringExtra2 = intent.getStringExtra("title");
        String stringExtra3 = intent.getStringExtra("message");
        int intExtra = intent.getIntExtra("smallBitmap", 0);
        String stringExtra4 = intent.getStringExtra("bigBitmap");
        String stringExtra5 = intent.getStringExtra("key");
        String stringExtra6 = intent.getStringExtra("activity");
        Boolean valueOf = Boolean.valueOf(intent.getBooleanExtra("sound", false));
        Boolean valueOf2 = Boolean.valueOf(intent.getBooleanExtra("vibrate", false));
        int intExtra2 = intent.getIntExtra("id", 0);
        try {
            cls = Class.forName(stringExtra6);
        } catch (ClassNotFoundException unused) {
            Log.d(TAG, "Unable to find App class name " + stringExtra6);
            try {
                cls = Class.forName(unityClassDefault);
            } catch (ClassNotFoundException unused2) {
                Log.d(TAG, "Unable to find App class name com.unity3d.player.UnityPlayerActivity");
                return;
            }
        } catch (NullPointerException unused3) {
            Log.d(TAG, "Unable to find App class name " + stringExtra6 + " and " + unityClassDefault);
            return;
        }
        Log.d(TAG, "Start check if class exists: com.unity3d.player.UnityPlayerActivity and " + stringExtra6);
        if (cls != null) {
            Log.d(TAG, "Notification for class " + cls.getName());
            Intent intent2 = new Intent(context, cls);
            intent2.addFlags(872415232);
            intent2.putExtra("fromNotification", true);
            intent2.putExtra("notificationTime", System.currentTimeMillis() + "");
            intent2.putExtra("key", stringExtra5);
            PendingIntent activity = PendingIntent.getActivity(context, intExtra2, intent2, 134217728);
            Notification.Builder builder = new Notification.Builder(context);
            builder.setAutoCancel(true);
            builder.setContentIntent(activity).setWhen(System.currentTimeMillis()).setAutoCancel(true).setContentTitle(stringExtra2).setContentText(stringExtra3);
            if (stringExtra != null && stringExtra.length() > 0) {
                builder.setTicker(stringExtra);
            }
            switch (intExtra) {
                case 0:
                    builder.setSmallIcon(R.drawable.cat_i);
                    break;
                case 1:
                    builder.setSmallIcon(R.drawable.check_i);
                    break;
                case 2:
                    builder.setSmallIcon(R.drawable.chest_i);
                    break;
                case 3:
                    builder.setSmallIcon(R.drawable.crystal_i);
                    break;
                case 4:
                    builder.setSmallIcon(R.drawable.flash_i);
                    break;
                case 5:
                    builder.setSmallIcon(R.drawable.i_i);
                    break;
                case 6:
                    builder.setSmallIcon(R.drawable.lamp_i);
                    break;
                case 7:
                    builder.setSmallIcon(R.drawable.plane_i);
                    break;
                case 8:
                    builder.setSmallIcon(R.drawable.tank_i);
                    break;
                case 9:
                    builder.setSmallIcon(R.drawable.target_i);
                    break;
                case 10:
                    builder.setSmallIcon(R.drawable.train_i);
                    break;
                default:
                    builder.setSmallIcon(R.drawable.i_i);
                    Log.d(TAG, "Unable to find small icon with id=" + intExtra);
                    break;
            }
            try {
                byte[] decode = Base64.decode(stringExtra4, 0);
                builder.setLargeIcon(BitmapFactory.decodeByteArray(decode, 0, decode.length));
            } catch (Exception e) {
                Log.d(TAG, "Unable to load big icon: " + e.getMessage());
            }
            if (valueOf.booleanValue()) {
                builder.setSound(RingtoneManager.getDefaultUri(2));
            }
            if (valueOf2.booleanValue()) {
                builder.setVibrate(new long[]{300, 300});
            }
            if (Build.VERSION.SDK_INT >= 26) {
                notificationManager = notificationManager2;
                notificationManager.createNotificationChannel(new NotificationChannel(MonitorLogServerProtocol.DEFAULT_SAMPLE_RATES_KEY, "Information from game", 3));
                builder.setChannelId(MonitorLogServerProtocol.DEFAULT_SAMPLE_RATES_KEY);
            } else {
                notificationManager = notificationManager2;
            }
            if (Build.VERSION.SDK_INT < 16) {
                notification = builder.getNotification();
            } else {
                notification = builder.build();
            }
            Log.d(TAG, "Notify " + intExtra2);
            notificationManager.notify(intExtra2, notification);
            return;
        }
        Log.d(TAG, "Unable to find App class name " + stringExtra6 + " and " + unityClassDefault);
    }

    public static void CancelNotification(int i) {
        Activity activity = UnityPlayer.currentActivity;
        AlarmManager alarmManager = (AlarmManager) activity.getSystemService(NotificationCompat.CATEGORY_ALARM);
        PendingIntent broadcast = PendingIntent.getBroadcast(activity, i, new Intent(activity, UnityNotificationManager.class), 134217728);
        if (broadcast != null) {
            broadcast.cancel();
            Log.d(TAG, "Cancel notification " + i);
        }
        alarmManager.cancel(broadcast);
    }

    public static void CancelAll() {
        Log.d(TAG, "Cancel all notifications");
        for (int i = 0; i < 32; i++) {
            CancelNotification(i);
        }
    }

    public static void ClearAll() {
        ((NotificationManager) UnityPlayer.currentActivity.getApplicationContext().getSystemService("notification")).cancelAll();
        Log.d(TAG, "Clear all notifications");
    }
}